package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.*;

public class CreateArticleServlet implements Serializable {
    public void service(HttpRequest request, HttpResponse response) {
        System.out.println("开始编辑...");
        String title = request.getParameter("title");
        String user = request.getParameter("user");
        String content = request.getParameter("content");
        System.out.println(title + "," + user + "," +content);
        if (title==null) {
            File file = new File("./webapps/myweb/createArticle_oo.html");
            response.setEntity(file);
            return;
        }
        File file = new File(title+".article");
        if (file.exists()) {
            file = new File("./webapps/myweb/createArticle_xx.html");
            response.setEntity(file);
            return;
        }
        User author = new User(title,user,content);
        try (
                FileOutputStream fos = new FileOutputStream(title+".article");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(author);
            file = new File("./webapps/myweb/createArticle_ok.html");
            response.setEntity(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文章编辑完成.");
    }
}